package com.nbgroup.web.role;

import com.nbgroup.dao.MenuRoleRepositoty;
import com.nbgroup.entity.Menu;
import com.nbgroup.entity.MenuRole;
import com.nbgroup.entity.Role;
import com.nbgroup.entity.User;
import com.nbgroup.service.MenuRoleService;
import com.nbgroup.service.MenuService;
import com.nbgroup.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 18545 on 2019/7/10.
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuRoleService menuRoleService;
    @Autowired
    private MenuRoleRepositoty menuRoleRepositoty;
    //添加
    @ResponseBody
    @RequestMapping(value = "/roleOnAdd", method = RequestMethod.POST)
    public Map<String,Object> roleOnAdd(Role user){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("Message",roleService.save(user));
        return map;
    }
    //修改
    @ResponseBody
    @RequestMapping(value = "/roleOnUpdate", method = RequestMethod.POST)
    public Map<String,Object> roleOnUpdate(Role user){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("Message",roleService.update(user));
        return map;
    }
    //删除
    @ResponseBody
    @RequestMapping(value = "/roleOnDelete", method = RequestMethod.POST)
    public Map<String,Object> roleOnDelete(String id){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("Message",roleService.delete(id));
        return map;
    }
    //根据id获取信息
    @ResponseBody
    @RequestMapping(value ="/getDateById/{id}",method = RequestMethod.GET)
    public Map<String,Object> getDateById(@PathVariable("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Role user = roleService.getInfoById(id);
        map.put("user",user);
        return map;
    }
    //根据id获取信息
    @ResponseBody
    @RequestMapping(value ="/getMenuById/{id}",method = RequestMethod.GET)
    public Map<String,Object> getMenuById(@PathVariable("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
//        Role user = roleService.getInfoById(id);
        List<Menu> menu = menuService.getMenu();
        MenuRole menus = menuRoleRepositoty.findByRoleId(Long.parseLong(id));
        if(menus==null){
            map.put("menus","");
        }else {
            map.put("menus",menus.getMenuId());
        }
        map.put("menu",menu);
        return map;
    }
    //根据id获取信息
    @ResponseBody
    @RequestMapping(value ="/setMenuById/{id}/{ids}",method = RequestMethod.GET)
    public Map<String,Object> setMenuById(@PathVariable("id") String id,@PathVariable("ids") String ids) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        menuRoleService.saveMenuRole(id,ids);
        map.put("meeage","设置成功");
        return map;
    }

}
